package com.guoguo.service.impl;


import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.entity.AcCateGroup;
import com.guoguo.entity.AcCategoryAlbum;
import com.guoguo.entity.Category;

import com.guoguo.entity.AcCategoryGroup;
import com.guoguo.entity.vo.AcCategoryGroupVO;
import com.guoguo.entity.vo.CategoryAlbumVO;
import com.guoguo.entity.vo.CategoryVO;
import com.guoguo.mapper.AcCateGroupMapper;
import com.guoguo.mapper.AcCategoryAlbumMapper;
import com.guoguo.mapper.CategoryMapper;
import com.guoguo.service.ICategoryService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/9 0009.
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //ICategoryDao接口
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AcCateGroupMapper cateGroupMapper;

    @Autowired
    private AcCategoryAlbumMapper acCategoryAlbumMapper;

    @Override
    public int UDByPrimaryKey(String cateId) {
        int countNum = 0;
        Category category = categoryMapper.selectByPrimaryKey(cateId);
        if (category != null) {


            AcCategoryGroup acCategoryGroup = new AcCategoryGroup();
            acCategoryGroup.setGroupId(category.getGroupId());
            acCategoryGroup.setCateId(cateId);
            List<AcCategoryGroup> acCategoryGroups = cateGroupMapper.findCategoryGroup(acCategoryGroup);
            if (acCategoryGroups.size() > 0) {
                int categoryGroup = cateGroupMapper.deleteCategoryGroup(cateId);
                logger.info("================>>>>>   删除关联分組数量：" + categoryGroup);
                countNum += categoryGroup;
            }
            int cateNum = categoryMapper.deleteByPrimaryKey(cateId);
            logger.info("================>>>>>   删除分类数量：" + cateNum);
            countNum += cateNum;
            logger.info("================>>>>>   删除数据总数量：" + cateNum);
        }
        return countNum;

    }

    @Override
    public PageBean<AcCategoryGroupVO> getCategory(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageBean(categoryMapper.getCategory());
    }

    @Override
    public List<CategoryVO> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    @Override
    public PageBean<AcCategoryGroupVO> selectBySortDesc(String cateName,Integer isShow,String groupId,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageBean(categoryMapper.selectBySortDesc(cateName ,isShow ,groupId));
    }

    @Override
    public PageBean<AcCategoryGroupVO> selectBySortAsc(String cateName,Integer isShow,String groupId,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        return new PageBean(categoryMapper.selectBySortAsc(cateName ,isShow ,groupId));
    }

    @Override
    public PageBean<AcCategoryGroupVO> selectByCreateTimeAsc(String cateName,Integer isShow,String groupId,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageBean(categoryMapper.selectByCreateTimeAsc(cateName ,isShow ,groupId));
    }

    @Override
    public PageBean<AcCategoryGroupVO> selectByCreateTimeDesc(String cateName,Integer isShow,String groupId,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        return new PageBean(categoryMapper.selectByCreateTimeDesc(cateName ,isShow ,groupId));
    }


    @Override
    public int insert(Category categorys) {
        return categoryMapper.insert(categorys);
    }

    @Override
    public int count(Category categorys) {
        return categoryMapper.count(categorys);
    }


    @Override
    public int insertSelective(Category categorys) {
        return categoryMapper.insertSelective(categorys);
    }


    @Override
    public Category selectByPrimaryKey(String cateId) {
        return categoryMapper.selectByPrimaryKey(cateId);
    }

    @Override
    public int deleteByPrimaryKey(String cateId) {
        return categoryMapper.deleteByPrimaryKey(cateId);
    }

    @Override
    public Category updateBySelective() {
        return categoryMapper.updateBySelective();
    }

    @Override
    public int updateCategory(Category category) {
        Date now = new Date();
        category.setUpdateTime(now);
        return categoryMapper.updateCategory(category);
    }

    @Override
    public Category addCategory(String cateId, String cateName, String cateDescription, String groupId, int sort,Date createTime, Date updateTime) {
        int countNum=0;
        Category category = categoryMapper.selectByPrimaryKey(cateId);
        List<CategoryAlbumVO> categoryAlbumVOs = acCategoryAlbumMapper.getCategoryAlbumList(cateId);
        if (category == null) {
            category = new Category(cateId, cateName, cateDescription, groupId, sort,createTime, updateTime);
            countNum +=categoryAlbumVOs.size();
            category.setArgs1(countNum);
            int num = categoryMapper.insertSelective(category);

        } else {
            category.setCateName(cateName);
            category.setCateDescription(cateDescription);
            category.setGroupId(groupId);
            category.setUpdateTime(updateTime);
            category.setSort(sort);
            countNum +=categoryAlbumVOs.size();
            category.setArgs1(countNum);
            int num = categoryMapper.updateByPrimaryKeySelective(category);


        }
        //统计分类下的专辑数


        //添加分类和分组之间的关联
        AcCategoryGroup acCategoryGroup = new AcCategoryGroup();
        acCategoryGroup.setGroupId(groupId);
        acCategoryGroup.setCateId(cateId);
        List<AcCategoryGroup> acCategoryGroups = cateGroupMapper.findCategoryGroup(acCategoryGroup);
        if (acCategoryGroups.size() > 0) {
            cateGroupMapper.updateCategoryGroup(acCategoryGroup);

        } else {
            cateGroupMapper.saveCategoryGroup(acCategoryGroup);
        }
        return category;
    }


}
